const app = getApp()
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js')
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    car:null,
    shop:null,
    tire_selection:null,
    l_f_tire:null,
    r_f_tire:null,
    l_e_tire:null,
    r_e_tire:null,
    date:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.init();
    that.initTyre();
  },
  init:function(){
    var that = this;
    var date = util.formatDate(new Date());
    let car  = xrSdk.cookie.get('car_info');
    let shop = xrSdk.cookie.get('shop_info');
    let tire_selection = xrSdk.cookie.get('tire_selection');
    that.setData({
      car: car,
      shop: shop,
      tire_selection: tire_selection,
      date: date
    });
  },
  // 从缓存中读取轮胎信息
  initTyre: function () {
    var that = this;
    var l_f_tire = xrSdk.cookie.get('l_f_tire');
    var r_f_tire = xrSdk.cookie.get('r_f_tire');
    var l_e_tire = xrSdk.cookie.get('l_e_tire');
    var r_e_tire = xrSdk.cookie.get('r_e_tire');
    that.setData({
      l_f_tire: l_f_tire,
      r_f_tire: r_f_tire,
      l_e_tire: l_e_tire,
      r_e_tire: r_e_tire
    });
  },
  nextClick:function(e){
    var that = this;
    var l_f_tire_json = JSON.stringify(that.data.l_f_tire);
    var r_f_tire_json = JSON.stringify(that.data.r_f_tire);
    var l_e_tire_json = JSON.stringify(that.data.l_e_tire);
    var r_e_tire_json = JSON.stringify(that.data.r_e_tire);
    xrRequest.request(app,{
      url: config.service.addTyre,
      login: true,
      data: {
        shop:that.data.shop.id,
        car:that.data.car.id,
        l_f_tire: l_f_tire_json == '""' ? "" : l_f_tire_json,
        r_f_tire: r_f_tire_json == '""' ? "" : r_f_tire_json,
        l_e_tire: l_e_tire_json == '""' ? "" : l_e_tire_json,
        r_e_tire: r_e_tire_json == '""' ? "" : r_e_tire_json,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          //wx.navigateBack();

          that.resetTireInfoCookie();
          
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.setData({
            'showSuccess': true
          }, function () {
            wx.navigateBack({
              delta: 1
            });
          });


          //最后就是返回上一个页面。
          // wx.navigateBack({
          //   delta: 1,  // 返回上一级页面。
          //   success: function () {
          //    wx.redirectTo({
          //     url: '../success/success'
          //   })
          //   }
          // });
          
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { 
        wx.showToast({
          title: res,
          icon: 'none',
        })
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 清空下缓存
   */
  resetTireInfoCookie: function () {
    xrSdk.cookie.remove('tire_selection');
    xrSdk.cookie.remove('l_f_tire');
    xrSdk.cookie.remove('r_f_tire');
    xrSdk.cookie.remove('l_e_tire');
    xrSdk.cookie.remove('r_e_tire');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})